package com.atguigu.product.products.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.product.products.dao.PmsCategoryBrandRelationDao;
import com.atguigu.product.products.entity.PmsBrandEntity;
import com.atguigu.product.products.vo.respVo.CategoryBrandRelationRespVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.product.products.entity.PmsCategoryBrandRelationEntity;
import com.atguigu.product.products.service.PmsCategoryBrandRelationService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:57:10
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class PmsCategoryBrandRelationController {
    @Autowired
    private PmsCategoryBrandRelationService pmsCategoryBrandRelationService;
    @Autowired
    private PmsCategoryBrandRelationDao pmsCategoryBrandRelationDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("products:pmscategorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pmsCategoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据三级分类id查询所有的品牌
     */
    @RequestMapping("/brands/list")
    public R brandsList(@RequestParam Long catId) {
        //封装vo得到返回数据

        List<PmsBrandEntity> pmsBrandEntities = pmsCategoryBrandRelationService.brandsList(catId);
        if (pmsBrandEntities == null) {
            return R.error("没有找到对应的品牌信息");
        }
        List<CategoryBrandRelationRespVo> categoryBrandRelationRespVos = pmsBrandEntities.stream().map((m) -> {
            CategoryBrandRelationRespVo relationRespVo = new CategoryBrandRelationRespVo();
            relationRespVo.setBrandId(m.getBrandId());
            relationRespVo.setBrandName(m.getName());

            return relationRespVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", categoryBrandRelationRespVos);
    }

    /**
     * 查询品牌分类下的所有列表查询
     */
    @RequestMapping("/catelog/list")
    //@RequiresPermissions("products:pmscategorybrandrelation:list")
    public R list(@RequestParam(value = "brandId", required = true) long brandId) {
        //PageUtils page = pmsCategoryBrandRelationService.queryPage(params);
        List<PmsCategoryBrandRelationEntity> pmsCategoryBrandRelationEntities = pmsCategoryBrandRelationDao.selectList(new QueryWrapper<PmsCategoryBrandRelationEntity>().eq("brand_id", brandId));
        return R.ok().put("data", pmsCategoryBrandRelationEntities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("products:pmscategorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        PmsCategoryBrandRelationEntity pmsCategoryBrandRelation = pmsCategoryBrandRelationService.getById(id);

        return R.ok().put("pmsCategoryBrandRelation", pmsCategoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("products:pmscategorybrandrelation:save")
    public R save(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation) {
        pmsCategoryBrandRelationService.saveDetail(pmsCategoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("products:pmscategorybrandrelation:update")
    public R update(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation) {
        pmsCategoryBrandRelationService.updateById(pmsCategoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("products:pmscategorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        pmsCategoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
